package com.mrd.gtimpframe.service.impl;

import com.mrd.gtimpframe.entity.CargoDeclaRecord;
import com.mrd.gtimpframe.entity.ComDeclaWay;
import com.mrd.gtimpframe.entity.Declaration;
import com.mrd.gtimpframe.repository.CargoDeclaRecordMapper;
import com.mrd.gtimpframe.service.CargoDeclaRecordService;
import com.mrd.redis.JedisClientClusterImpl;
import com.mrd.util.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CargoDeclaRecordServiceImpl implements CargoDeclaRecordService {
    @Autowired
    private CargoDeclaRecordMapper cargoDeclaRecordMapper;
    @Autowired
    private JedisClientClusterImpl jedisClientCluster;

    @Override
    public int deleteByPrimaryKey(String id) {
        return cargoDeclaRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(CargoDeclaRecord record) {
        return cargoDeclaRecordMapper.insert(record);
    }

    @Override
    public int insertSelective(CargoDeclaRecord record) {
        return cargoDeclaRecordMapper.insertSelective(record);
    }

    @Override
    public CargoDeclaRecord selectByPrimaryKey(String id) {
        return cargoDeclaRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(CargoDeclaRecord record) {
        return cargoDeclaRecordMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(CargoDeclaRecord record) {
        return updateByPrimaryKey(record);
    }

    @Override
    public CargoDeclaRecord selectByDidAndUploadWay(String did, String uploadWay) {
        return cargoDeclaRecordMapper.selectByDidAndUploadWay(did, uploadWay);
    }

    @Override
    public CargoDeclaRecord selectByBusinessAndUploadWay(String businessNo, String uploadWay) {
        return cargoDeclaRecordMapper.selectByBusinessAndUploadWay(businessNo, uploadWay);
    }

    @Override
    public CargoDeclaRecord selectByInvNoAndUploadWay(String invNo, String uploadWay) {
        return cargoDeclaRecordMapper.selectByInvNoAndUploadWay(invNo, uploadWay);
    }

    @Override
    public List<CargoDeclaRecord> selectByUploadWayAndStatusNotIn(String uploadWay, List<String> status) {
        return cargoDeclaRecordMapper.selectByUploadWayAndStatusNotIn(uploadWay, status);
    }

    @Override
    public int batchUpdateByPrimaryKey(List<CargoDeclaRecord> list) {
        return cargoDeclaRecordMapper.batchUpdateByPrimaryKey(list);
    }

    @Override
    public List<CargoDeclaRecord> selectByDid(List<String> dids) {
        return cargoDeclaRecordMapper.selectByDid(dids);
    }

    /**
     * 新增记录并存入待上传队列
     */
    @Override
    public CargoDeclaRecord newRecord(Declaration declaration, String uploadWay, String comId) {
        CargoDeclaRecord record = new CargoDeclaRecord();
        record.setId(StringUtils.getUUID());
        record.setCreateTime(new Date());
        record.setStatus("TM");
        record.setUploadWay(ComDeclaWay.WayCodeEnum.COM_PORT_FTP.toString());
        record.setDid(declaration.getId());
        record.setBusinessNo(declaration.getBusinessId());
        record.setCompanyId(comId);
        record.setIeFlag(Declaration.DECLARATION_TYPE_EXPORT
                .equals(declaration.getDeclarationType()) ? "1" : "0");
        record.setTrnFlag("直航".equals(declaration.getDataType()) ? "0" : "1");
        record.setState("0");
        int i = cargoDeclaRecordMapper.insert(record);
        jedisClientCluster.leftPush("ComDeclationWaitingUpload", comId + "-" + declaration.getId());
        if (i == 1) {
            return record;
        } else {
            return null;
        }
    }
}
